import { Component, NgZone } from '@angular/core';
import { IonicPage, ToastController } from 'ionic-angular';
import { Tools } from '../../tools/tools';
import { NativePageTransitions } from '@ionic-native/native-page-transitions';
import { Router } from '@angular/router';
// import { AppVersion } from '@ionic-native/app-version';
import { Network } from '@ionic-native/network';

/**
 * Generated class for the AboutUsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-about-us',
  templateUrl: 'about-us.html',
})
export class AboutUsPage extends Tools{
  private title:string = "关于我们" ;
  private versionnumber:string = '2.0.0'
  private showAppUpdate:boolean; // 是否显示更新进度条
  private processSpeed:string; // 下载进度的百分值
  private hasNewVersion:boolean = false;

  constructor(
    // private appVersion: AppVersion,
    protected route: Router,
    protected nativePageTransitions: NativePageTransitions,
    protected network: Network,
    private zone: NgZone,
    private toastCtrl: ToastController
  ) {
    super(route,nativePageTransitions, network)
    //  自动条用检查更新接口，提示有新版本
    if (this.isMobile()) {
      this.onlyCheckUpdate(() => {
        // 有新版本
        this.hasNewVersion = true
      })
    }
  }

  back() {
    this.openPage('settings')
  }

  checkUpdate(flag){
    // 检查更新
    console.log('检查更新');
    let that = this;
    /**
     * 检查更新
     * downsuccess,downerror,installsuccess,installerr,noupdate
     * @param {[type]} downsuccess 下载进度更新
     * @param {[type]} downerror 下载失败
     * @param {[type]} installsuccess 安装成功
     * @param {[type]} installerr 安装失败
     * @param {[type]} noupdate 暂无更新
     */

    this.checkUpdateApp((processObj) => {
      // 更新进度条
      that.zone.run(() => {
        that.showAppUpdate = true;
        that.processSpeed = processObj.receivedBytes / processObj.totalBytes * 100 + '%';
      })
      // 下载完关闭 进度框
      if (processObj.receivedBytes == processObj.totalBytes) {
        that.zone.run(() => {
          that.showAppUpdate = false;
        })
      }
    },() => {
      that.toastCtrl.create({
        message: '下载安装包有误',
        position: 'top',
        duration: 1000
      }).present();
    },() => {
      that.toastCtrl.create({
        message: '安装成功',
        position: 'top',
        duration: 1000
      }).present();
    }, () => {
      that.toastCtrl.create({
        message: '安装失败',
        position: 'top',
        duration: 1000
      }).present();
    }, () => {
      that.toastCtrl.create({
        message: '暂无新版本',
        position: 'top',
        duration: 1000
      }).present();
    })
    
  }
}
